package com.fram.service.api.v1.impl;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fram.bean.api.distributor.response.DistributorFarmResponse;
import com.fram.bean.api.project.request.CategoryRequest;
import com.fram.bean.api.project.request.OrderCommentRequest;
import com.fram.bean.api.project.request.ProjectGoodsSearchRequest;
import com.fram.bean.api.project.response.CategoryResponse;
import com.fram.bean.api.project.response.OrderCommentResponse;
import com.fram.bean.api.project.response.ProjectGoodsDetailResponse;
import com.fram.bean.api.project.response.ProjectGoodsSearchResponse;
import com.fram.bean.sys.PageBean;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProAffairException;
import com.fram.common.exception.ProException;
import com.fram.common.exception.ProRunTimeException;
import com.fram.common.exception.ProValiDataException;
import com.fram.common.util.base.BaseService;
import com.fram.entity.ProjectGoodsPrice;
import com.fram.entity.UserBrowsingRecords;
import com.fram.mapper.DistributorFarmMapper;
import com.fram.mapper.DistributorProjectCategoryMapper;
import com.fram.mapper.OrderCommentMapper;
import com.fram.mapper.ProjectGoodsMapper;
import com.fram.mapper.ProjectGoodsPriceMapper;
import com.fram.service.api.v1.ProjectService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class ProjectServiceImpl extends BaseService implements ProjectService {

	@Autowired
	private ProjectGoodsMapper projectGoodsMapper;
	@Autowired
	private ProjectGoodsPriceMapper projectGoodsPriceMapper;
	@Autowired
	private DistributorProjectCategoryMapper projectCategoryMapper;
	@Autowired
	private OrderCommentMapper orderCommentMapper;
	@Autowired
	private DistributorFarmMapper distributorFarmMapper;

	/**
	 * 1.商品根据 店铺 以及分类 展示 默认1级展示 商品（改功能包含 分页查询 分类查询 模糊查询） 2.null 该页面是 针对店铺 下的所有商品
	 */
	@Override
	public PageInfo<ProjectGoodsSearchResponse> queryProjectGoodsList(PageBean<ProjectGoodsSearchRequest> bean) throws ProException {
		// 分页插件的 必备
		PageHelper.startPage(bean.getPageNum(), bean.getPageSize());
		// 功能实现
		List<ProjectGoodsSearchResponse> list = projectGoodsMapper.listForApi(bean.getParam());
		// 判断list 是否有值
		if (list == null || list.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		// 返回分页数据
		return new PageInfo<>(list);
	}

	/**
	 * 针对店铺id 查询所有 改店铺的 类型 （1级 2级）
	 */
	@Override
	public List<CategoryResponse> selectProductCategoryList(CategoryRequest request) throws ProException {
		List<CategoryResponse> categoryResponse = projectCategoryMapper.selectProductCategoryList(request);
		if (categoryResponse == null || categoryResponse.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return categoryResponse;
	}

	@Override
	public ProjectGoodsDetailResponse projectGoodsDetail(Long id, Long userId) throws ProException {
		ProjectGoodsDetailResponse goods = projectGoodsMapper.detailApi(id, userId);
		if (goods == null) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		// 记录浏览记录
		insertBrowsingRecords(new UserBrowsingRecords(goods.getGoodsType(), goods.getId(), "产品、套餐"));
		return goods;
	}

	@Override
	public PageInfo<OrderCommentResponse> projectComment(PageBean<OrderCommentRequest> request) throws ProException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<OrderCommentResponse> list = orderCommentMapper.projectComment(request.getParam());
		if (list == null || list.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}

	@Override
	@Transactional
	public void projectStatus(Long adminUserId, Long goodsId, Integer status) throws ProAffairException {
		DistributorFarmResponse distributor = distributorFarmMapper.selectByAdminUserId(adminUserId);
		if (distributor == null) {
			throw new ProRunTimeException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		com.fram.bean.admin.goods.response.GoodsResponse goods = projectGoodsMapper.selectByPrimaryKey(goodsId);
		if (goods == null || distributor.getId().longValue() != goods.getDistributorId().longValue()) {
			throw new ProRunTimeException(ErrCode.ERR_INVALIDSECRET_NO_EXISTED.getReturnCode(), ErrCode.ERR_INVALIDSECRET_NO_EXISTED.getReturnMsg());
		}
		ProjectGoodsPrice request = new ProjectGoodsPrice();
		request.setGoodsId(goodsId);
		request.setStatus(status);
		if (projectGoodsPriceMapper.updateByGoodsPriceStatus(request) <= 0) {
			throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

}
